package org.imaginedays.androidall.xml.parsers;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.imaginedays.androidall.error.AndroidAllError;
import org.imaginedays.androidall.error.AndroidAllParseException;
import org.imaginedays.androidall.types.User;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class UserParser extends AbstractParser<User> {

	private static final Logger LOG = Logger.getLogger(UserParser.class
			.getCanonicalName());

	@Override
	protected User parseInner(XmlPullParser parser) throws IOException,
			XmlPullParserException, AndroidAllError, AndroidAllParseException {
		parser.require(XmlPullParser.START_TAG, null, null);
		User user = new User();
		while (parser.nextTag() == XmlPullParser.START_TAG) {
			String name = parser.getName();
			if (User.UID.equals(name)) {
				user.uId = parser.nextText();
			} else if (User.UNAME.equals(name)) {
				user.uName = parser.nextText();
			} else if (User.UADDRESS.equals(name)) {
				user.uAddress = parser.nextText();
			} else if (User.UAGE.equals(name)) {
				user.uAge = parser.nextText();
			} else {
				if (DEBUG)
					LOG.log(Level.FINE, "Found tag that we don't recognize: "
							+ name);
				skipSubTree(parser);
			}

		}

		return user;
	}

}
